package com.xs.shop.logistics.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xs.shop.common.exceptions.BusinessException;
import com.xs.shop.common.exceptions.enums.Resp;
import com.xs.shop.logistics.model.entity.Logistics;
import com.xs.shop.logistics.service.mapper.LogisticsMapper;
import com.xs.shop.logistics.service.service.LogisticsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xs
 * @since 2025-07-16
 */
@Service
public class LogisticsServiceImpl extends ServiceImpl<LogisticsMapper, Logistics> implements LogisticsService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Logistics addLogistics(Logistics logistics) {
        // 设置默认值
        if (logistics.getStatus() == null) {
            logistics.setStatus((byte) 0); // 默认待发货
        }
        if (logistics.getIsDeleted() == null) {
            logistics.setIsDeleted((byte) 0); // 默认未删除
        }
        logistics.setCreatedAt(new Date());
        logistics.setUpdatedAt(new Date());

        this.save(logistics);
        return logistics;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteLogistics(Long id) {
        if (id == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Logistics updateLogistics(Logistics logistics) {
        if (logistics == null || logistics.getId() == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        logistics.setUpdatedAt(new Date());
        this.updateById(logistics);
        return logistics;
    }

    @Override
    public Logistics getLogisticsById(Long id) {
        if (id == null) {
            throw new BusinessException(Resp.INVALID_PARAM);
        }
        return this.getById(id);
    }

    @Override
    public Page<Logistics> pageLogistics(Integer current, Integer pageSize,
                                         String logisticsNo, Long orderId, String orderNo,
                                         String receiverName, String receiverPhone) {
        LambdaQueryWrapper<Logistics> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.hasText(logisticsNo), Logistics::getLogisticsNo, logisticsNo)
                .eq(orderId != null, Logistics::getOrderId, orderId)
                .like(StringUtils.hasText(orderNo), Logistics::getOrderNo, orderNo)
                .like(StringUtils.hasText(receiverName), Logistics::getReceiverName, receiverName)
                .like(StringUtils.hasText(receiverPhone), Logistics::getReceiverPhone, receiverPhone);

        // 按创建时间倒序
        queryWrapper.orderByDesc(Logistics::getCreatedAt);

        return this.page(new Page<>(current, pageSize), queryWrapper);
    }
}
